import os
import pandas as pd
import numpy as np
import os

os.getcwd()

data = pd.read_csv("market_projectcontracts.csv")
data1 = pd.read_csv("market_project.csv")
data3 = pd.read_csv("market_activity.csv")
data4 = pd.read_csv("market_trade.csv")

class Sta():

    def __init__(self, activity_id):
        self.activity_id = activity_id

    def id_group(self):
        a = data4.loc[:, ['user_id', 'activity_id', 'project_id', 'contract_id', 'group_id']]
        b = a.drop_duplicates(['user_id'])
        return b

    def trade_count(self):
        return data4.groupby(['user_id'],as_index=False)['user_id'].agg({'trade_count':'count'})

    def contract_text(self):
        a = data.loc[:, ['id', 'contract_text']]
        return a

    def trade_buy_count(self):
        a = data4[data4['action_type'] == 'buy']
        return a.groupby(['user_id'],as_index=False)['action_type'].agg({'trade_buy_count':'count'})

    def experiment_id(self):
        return data3.loc[:, ['id', 'experiment_id']]

    def share_group(self):
        a = data4.groupby("user_id")["share"].agg([("share_max", "max"), ("share_min", "min"),("share_mean","mean")])
        return a.reset_index()

    def self_prob_group(self):
        a = data4.groupby('user_id')['pred_proba'].agg([('self_prob_max', 'max'), ('self_prob_min', 'min'), ('self_prob_mean', 'mean')])
        return a.reset_index()

    def confidence_group(self):
        b = pd.DataFrame(data4.loc[:, ['user_id','trade_confidence']], columns=['user_id', 'trade_confidence'])
        class_mapping = {'完全瞎猜':0, '有点瞎猜':1, '有点信心':2, '很有信心':3}
        b['trade_confidence'] = b['trade_confidence'].map(class_mapping)
        b = b.groupby('user_id')['trade_confidence'].agg([('confidence_max', 'max'), ('confidence_min', 'min'), ('confidence_mean', 'mean')])
        return b.reset_index()

    def contect_2(self,activity_id):
        df1 = pd.merge( pd.DataFrame(self.id_group()),pd.DataFrame(self.trade_count()), how='outer', on='user_id')
        df1 = pd.merge(pd.DataFrame(df1),pd.DataFrame(self.contract_text()), how='outer',left_on='contract_id',right_on='id').drop(columns=['id'])
        df2 = pd.merge(pd.DataFrame(df1),pd.DataFrame(self.share_group()), how='outer',on='user_id')
        df1 = pd.merge(pd.DataFrame(df2),pd.DataFrame(self.experiment_id()), how='outer',left_on='activity_id',right_on='id').drop(columns=['id'])
        df2 = pd.merge(pd.DataFrame(df1),pd.DataFrame(self.trade_buy_count()),how='outer',on='user_id')
        df1 = pd.merge(pd.DataFrame(df2),pd.DataFrame(self.confidence_group()),how='outer',on='user_id')
        df2 = pd.merge(pd.DataFrame(df1),pd.DataFrame(self.self_prob_group()), how='outer',on='user_id')
        df3 = df2.loc[df2['activity_id'] == activity_id ]
        return df3.to_csv('Result.csv')


sta = Sta('6')
print(sta.contect_2(6))




